//
//  UIAlertTableView.m
//  MobilCommunity
//
//  Created by easystudio on 7/21/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MoreChangePosi.h"
#import "Session.h"
#import "InfoType.h"
#import "TaskManager.h"
#import "XmlRpc.h"
#import "DHCUtil.h"

#define kTablePadding 8.0f

@interface UIAlertView 

(private)
- (void)layoutAnimated:(BOOL)fp8;

@end

@implementation MoreChangePosi

@synthesize lastIndexPath;
@synthesize tableHeight;
@synthesize table;

- (id)init
{    
	self = [super init];
    
   	if(self)
	{
        tableHeight = 200;
        responseParams = [[NSMutableArray alloc] initWithCapacity:0];        
	}
  
    [self prepare];    

	return self;
}

- (void)layoutAnimated:(BOOL)fp8 
{    
    [super layoutAnimated:fp8];    
    [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y - tableExtHeight/2, self.frame.size.width, self.frame.size.height + tableExtHeight)];        
    // We get the lowest non-control view (i.e. Labels) so we can place the table view just below    
    UIView *lowestView = nil;    
    int i = 0;    
    while (![[self.subviews objectAtIndex:i] isKindOfClass:[UIControl class]]) 
    {        
        lowestView = [self.subviews objectAtIndex:i];
        i++;    
    }        
    CGFloat tableWidth = 262.0f;  
    if (lowestView != nil)
    {
        table.frame = CGRectMake(11.0f, lowestView.frame.origin.y + lowestView.frame.size.height + 2 * kTablePadding, tableWidth, tableHeight); 
    }
       
    for (UIView *sv in self.subviews) {        
        // Move all Controls down        
        if ([sv isKindOfClass:[UIControl class]]) {            
            sv.frame = CGRectMake(sv.frame.origin.x, sv.frame.origin.y + tableExtHeight, sv.frame.size.width, sv.frame.size.height);        
        }    
    }    
}
//rewrite the show method of UIAlertView
- (void)show
{
    [super show];
}
- (void)prepare 
{    
    [[[TaskManager Share] GetTaskByType:GetMemberPosiList Delegate:self] Excute];
        
    if (tableHeight == 0) {
        tableHeight = 150.0f;    
    }        
    tableExtHeight = tableHeight + 2 * kTablePadding;        
    table = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f) style:UITableViewStylePlain];    
    table.delegate = self;    
    table.dataSource = self;        
    table.backgroundColor = [UIColor clearColor];
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self insertSubview:table atIndex:0];        
    [self setNeedsLayout];
}

- (void)dealloc {
    [responseParams release];
    [lastIndexPath release];
    [table release];    
    [super dealloc];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [responseParams count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.accessoryType = UITableViewCellAccessoryNone;
        
    NSUInteger row = [indexPath row];
    if ([responseParams count] <= row)
        return cell;

    NSString *name=[NSString stringWithFormat:@"%@",[[responseParams objectAtIndex:row] objectForKey:@"position_name"]];
//    NSLog(@"name=%@",name);
    cell.accessoryType=UITableViewCellAccessoryNone;
    
    if ([name isEqualToString:[[Session share] getMemberPosiName]]){
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
        [cell setHighlighted:YES];
        [cell setSelected:YES];
        self.lastIndexPath = indexPath;
//        UIView *view=[[UIView alloc] init];//1E90FF
//        [view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"select_bg"]]];
//        cell.backgroundView = view;
//        [view release];
        
    }
    
    cell.textLabel.text = name;//[InfoListTitle objectAtIndex:row];
    
    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    int newRow = [indexPath row];
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
    
    if ([responseParams count] <= newRow)
        return;
    
    currentSelectIndex=newRow;
    
    if (newRow != oldRow)
    {
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath]; 
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        [oldCell setHighlighted:NO];
        [oldCell setSelected:NO];
//        UIView *view=[[UIView alloc] init];//1E90FF
//        [view setBackgroundColor:[UIColor clearColor]];
//        oldCell.backgroundView = view;
//        [view release];
        self.lastIndexPath = indexPath;

        UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
        
        posi_id=[NSString stringWithFormat:@"%@",[[responseParams objectAtIndex:newRow] objectForKey:@"id"]];
        posi_name=[NSString stringWithFormat:@"%@",[[responseParams objectAtIndex:newRow] objectForKey:@"position_name"]];
        permission=[NSString stringWithFormat:@"%@",[[responseParams objectAtIndex:newRow] objectForKey:@"permission"]];
        
        ///
        [[[Session share] getMember] setPosi_id:posi_id];
        [[[Session share] getMember] setPosi_name:posi_name];
        if ([permission isEqualToString:@"0"])
            [[[Session share] getMember] setPermission:NO];
        else
            [[[Session share] getMember] setPermission:YES];

    }
}


#pragma mark - my fun

-(void)setResponseParams:(NSMutableArray *)params
{
    [responseParams removeAllObjects];
    responseParams = params;
}

-(int)getCurrentSelectIndex
{
    return currentSelectIndex;
}

//
-(void) OnPreExcute
{
}

-(void) DoInBackground
{
    NSMutableArray *tmp=nil;
    XmlRpc *xml = [[XmlRpc alloc] init];
    
    tmp = [xml GetMemberPosi];
    
    for (NSMutableDictionary* itq in tmp) {
        [responseParams addObject:itq];
    }
    
    [self.table reloadData];
    [xml release];
    
}

-(void) OnPostExcute
{
}

-(NSString *)getPosi_id
{
    return posi_id;
}
-(NSString *)getPosi_name
{
    return posi_name;
}
-(NSString *)getPermission
{
    return permission;
}


@end

